<template>
  <van-nav-bar
    title="登录"
    left-text="返回"
    left-arrow
    @click-left="$router.back()"
    placeholder
    fixed
  />
  <van-form>
    <van-cell-group inset>
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ pattern: /\w{3,}/, message: '三位以上 的数字字母下划线' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ pattern: /.{6,}/, message: '三位以上 的任意字符' }]"
      />
    </van-cell-group>
    <div style="margin: 16px">
      <van-button round block type="primary" native-type="submit" @click="loginFn()">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { login } from '@/api/user'
let router = useRouter()
let username = ref('')
let password = ref('')
function loginFn() {
  login(username.value, password.value).then((res) => {
    console.log(res)
    if (res.data.code == 1) {
      let token = localStorage.setItem('token', res.data.data)
      router.push('/')
      showSuccessToast('登录成功')
    } else {
      showFailToast('登录失败')
    }
  })
}
</script>

<style></style>
